export default {
    name: "app",
    data() {
        return {
            inbound: {
                warehouseName: '',
                realName: '',
                time: '',
                source: '',
                remark: '',
                inventoryVMList: [],
            },

            // 仓库选项
            warehouseList: [],
            // 商品列表
            goodsList: [],

            // 表头
            tableColumnList: [
                { prop: 'goodsId', propName: '货号' },
                { prop: 'goodsName', propName: '货物名' },
                { prop: 'color', propName: '色号' },
                { prop: 'size', propName: '尺寸' },
                { prop: 'number', propName: '数量' },
                { prop: 'price', propName: '价格' }
            ],
            tableColumnList2: [
                { prop: 'goodsId', propName: '货号' },
                { prop: 'goodsName', propName: '货物名' },
                { prop: 'color', propName: '色号' },
                { prop: 'size', propName: '尺寸' },
                { prop: 'price', propName: '价格' },
                { prop: 'number', propName: '库存' },
            ],
            // 表格数据
            showList: [],
            // 分页
            currentPage: 1,
            total: 0,
            pageSize: 5,

            // 弹窗
            add_inbound_goods_dialog: false,
            // 选中商品信息
            goods: {},
            // 增添商品信息
            inventory: {
                goodsId: '',
                number: ''
            },
            boundstate: true,
        }
    },
    created() {
        const that = this
        setTimeout(function () {
            if (window.sessionStorage.getItem("boundstate") == "check") {
                that.boundstate = false
                that.inbound = that.$qs.parse(that.$route.query.detail)
                that.get_inbound_detail()
                that.pageSize = 6
            }
            else {
                that.boundstate = true
                that.get_warehouseList()
                that.get_goodsList()
                that.inbound.realName = that.$store.state.current.realName
                that.pageSize = 5
            }
        }, 400);
    },
    methods: {
        /* 对新增单据的操作 */
        // 获取仓库列表
        async get_warehouseList() {
            const { data: res } = await this.$http.get('/gim/api/admin/manageWarehouse/selectWarehouse/allWarehouse');
            if (res.code == 1)
                this.warehouseList = res.response
            else
                this.$store.commit('error', '获取仓库列表信息失败')
        },
        // 获取货物列表
        async get_goodsList() {
            const inquire_info = {
                gid: '',
                goodsName: '',
                size: '',
                minPrice: '',
                maxPrice: '',
            }
            const inquire = this.$qs.parse(inquire_info)
            const { data: res } = await this.$http.post('/gim/api/admin/manageGoods/selectGoods', inquire);
            // console.log(res)
            if (res.code == 1)
                this.goodsList = res.response.goodsSelectList
            else
                this.$store.commit('error', '获取货物列表信息失败')
        },
        // 打开新增弹窗
        open_add_dialog() {
            this.add_inbound_goods_dialog = true
        },
        // 获得选择货物的具体信息
        getGoods(item) {
            this.goods = item
            // console.log(item)
        },
        // 添加货物进明细表
        add_inbound_goods() {
            if (this.inventory.goodsId == '') {
                this.$store.commit('error', '商品选择不可为空')
            }
            else if (this.inventory.number == 0 || this.inventory.number == '') {
                this.$store.commit('error', '添加数量不可为零')
            }
            else {
                var j = 0
                for (var i = 0; i < this.showList.length; i++) {
                    if (this.showList[i].goodsId == this.inventory.goodsId) {
                        this.showList[i].number = parseInt(this.showList[i].number) + parseInt(this.inventory.number)
                        this.inbound.inventoryVMList[i].number = parseInt(this.inbound.inventoryVMList[i].number) + parseInt(this.inventory.number)
                    }
                    else j++
                }
                if (j >= this.showList.length) {
                    // this.goods.number = this.inventory.number
                    this.showList.push({
                        goodsId: this.inventory.goodsId,
                        goodsName: this.goods.goodsName,
                        color: this.goods.color,
                        size: this.goods.size,
                        number: this.inventory.number,
                        price: this.goods.price,
                    })
                    // model绑定的数据是对象属性，push进数组的数据也会跟着变化
                    this.inbound.inventoryVMList.push({ goodsId: this.inventory.goodsId, number: this.inventory.number })
                    this.total++
                }
            }

            // 重置
            this.goods = {}
            this.inventory = {
                goodsId: '',
                number: ''
            }
            this.add_inbound_goods_dialog = false
        },
        // 删除明细表中货物
        delete_inbound_goods(item) {
            for (var i = 0; i < this.showList.length; i++) {
                if (this.showList[i] == item) {
                    //从i出开始删除1个元素
                    this.showList.splice(i, 1)
                    this.inbound.inventoryVMList.splice(i, 1)
                    i--
                    this.total--

                    // 当总数为5的倍数时，需要改变当前页码
                    // this.total != 0很重要！因当删除为空时，this.currentPage按照下面的计算会变为0，后续增加数据会影响显示
                    if (this.total % 5 == 0 && this.total != 0) {
                        this.currentPage = this.total / 5
                    }
                }
            }
        },
        // 提交新增入库单
        async submit_new_inbound() {
            if (this.inbound.realName == '') {
                this.$store.commit('error', '经办人姓名不能为空，请先完善个人信息。')
            }
            else if (this.inbound.warehouseName == '' || this.inbound.time == '' || this.inbound.source == '' || this.inbound.remark == '') {
                this.$store.commit('error', '请完善入库单信息！')
            }
            else if (this.inbound.inventoryVMList == '') {
                this.$store.commit('error', '入库货物不能为空！')
            }
            else {
                this.inbound.time = this.$moment(this.inbound.time).valueOf();
                const inbound = this.$qs.parse(this.inbound)
                const { data: res } = await this.$http.post('/gim/api/admin/manageInventory/inputInventory', inbound);
                if (res.code == 1) {
                    this.$store.commit('success', res.response)
                    this.$router.push('/inbound_manage')
                }
                else {
                    this.$store.commit('error', res.response)
                }
            }
        },

        /* 对查看单据的操作 */
        async get_inbound_detail() {
            const { data: res } = await this.$http.post('/gim/api/admin/manageInventoryRecord/selectRecordItem/' + this.inbound.recordId);
            // console.log(res)
            if (res.code == 1) {
                this.total = res.response.totalNum
                this.tableColumnList = res.response.tableColumnList
                this.showList = res.response.inventoryRecordItemList
            }
            else
                this.$store.commit('error', '获取详情列表信息失败')
        },

        /* 对表格的操作 */
        //对el-table的表头，表格的样式进行设置
        tableHeaderCellStyle() {
            return 'background-color: #2c9678; color: #ffffff; border-color: white;'
        },
        tableCellStyle() {
            return 'border-color: #2c9678;'
        },
        // 页数变化
        handleCurrentChange(val) {
            this.currentPage = val;
        },
    },
}